package com.egene.tw;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.text.TextUtils;
import android.util.Log;

import com.egene.tw.data.RealmHelper;
import com.egene.tw.data.entity.DeviceSetting;
import com.weike.manager.CommandManager;

@SuppressLint("NewApi")
public class NotificationMonitorService extends NotificationListenerService {

    // 在收到消息时触发
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        // TODO Auto-generated method stub
        Bundle extras = sbn.getNotification().extras;
        // 获取接收消息APP的包名
        String notificationPkg = sbn.getPackageName();
        // 获取接收消息的抬头
        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
        // 获取接收消息的内容
        String notificationText = extras.getString(Notification.EXTRA_TEXT);
        Log.e("XSL_Test", "notificationPkg : "+notificationPkg);
        Log.e("XSL_Test", "notificationTitle : "+notificationTitle);
        Log.e("XSL_Test", "notificationText : "+notificationText);

        if (TextUtils.isEmpty(notificationPkg)) {
            return;
        }
        if (TextUtils.isEmpty(notificationText)) {
            return;
        }
        DeviceSetting deviceSetting = RealmHelper.loadSettings();
        if (deviceSetting.isDisremind()) {
            return;
        }
        int messageId = -1;
        switch (notificationPkg) {
            case "com.android.incallui"://call
            case "com.google.android.dialer"://call
                messageId = 1;
                break;
            case "com.google.android.apps.messaging"://sms
            case "com.android.mms"://sms
                messageId = 3;
                break;
            case "com.android.email"://email
                messageId = 4;
                break;
            case "com.android.calendar"://calendar
                messageId = 5;
                break;

            case "com.tencent.mm"://wechat
                messageId = 9;
                break;
            case "com.tencent.mobileqq"://mobileqq
                messageId = 7;
                break;
            case "com.sina.weibo"://sina
                messageId = 19;
                break;
            case "com.facebook.katana"://facebook
                messageId = 16;
                break;
            case "com.twitter.android"://twitter
                messageId = 15;
                break;
            case "com.whatsapp"://whatsapp
                messageId = 10;
                break;
            case "jp.naver.line.android"://line
                messageId = 14;
                break;
            case "com.kakao.talk"://kakao
                messageId = 20;
                break;

            case "com.skype.raider"://skype
                messageId = 8;
                break;
            case "com.google.android.gm"://google 邮箱
                messageId = 11;
                break;
        }
        if (messageId == -1) {
            return;
        }
        CommandManager manager = CommandManager.getInstance(this);
        if (manager != null) {
            manager.smartWarnInfo(messageId, 2, notificationTitle + ":" + notificationText);
        }

        System.out.println("nononono type: " + messageId);
        System.out.println("nononono title: " + notificationTitle);
        System.out.println("nononono msg: " + notificationText);
    }

    // 在删除消息时触发
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        // TODO Auto-generated method stub
        Bundle extras = sbn.getNotification().extras;
        // 获取接收消息APP的包名
        String notificationPkg = sbn.getPackageName();
        // 获取接收消息的抬头
        String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
        // 获取接收消息的内容
        String notificationText = extras.getString(Notification.EXTRA_TEXT);
        Log.i("XSL_Test", "Notification removed " + notificationTitle + " & " + notificationText);
    }
}